package com.appheader.rongmaphone.utils;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;

import java.io.File;

public class CameraUtils {
	private static String filePath = null;
	public static void goClipping(Activity act, int requestCode, Uri sourceUri, Uri targetUri){
		if(act == null || sourceUri == null || targetUri == null || requestCode <= 0) return;
		Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(sourceUri, "image/*");
        intent.putExtra("crop", "true");
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("outputX", 80);
        intent.putExtra("outputY", 80);
        intent.putExtra("noFaceDetection", true);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, targetUri);
        intent.putExtra("return-data", false);
        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
        
        act.startActivityForResult(intent, requestCode);
	}
	
	public static void goTakingPhotos(Activity act, int requestCode, Uri uri){
		goCamera(act, MediaStore.ACTION_IMAGE_CAPTURE, requestCode, uri);
	}
	
	public static void goRecordingVideos(Activity act, int requestCode, Uri uri){
//		Intent intent=new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
//		intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
//		intent.putExtra(android.provider.MediaStore.EXTRA_SIZE_LIMIT, 768000);
//		intent.putExtra( android.provider.MediaStore.EXTRA_DURATION_LIMIT, 45000); 
//		act.startActivityForResult(intent, requestCode);
		

		Intent intent=new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
		intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
		//intent.putExtra(android.provider.MediaStore.EXTRA_SIZE_LIMIT, 768000);
		//intent.putExtra( android.provider.MediaStore.EXTRA_DURATION_LIMIT, 45000); 
		intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0.1);
		act.startActivityForResult(intent, requestCode);
	
	}
	
	public static String takePhoto(final Activity activity, final String dir, final String filename, final int cmd) {
		filePath = dir + filename;

		final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

		final File cameraDir = new File(dir);
		if (!cameraDir.exists()) {
			System.out.println("文娟加不存在");
			return "";
		}

		final File file = new File(filePath);
		final Uri outputFileUri = Uri.fromFile(file);
		intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
		try {
			activity.startActivityForResult(intent, cmd);
		} catch (final ActivityNotFoundException e) {
			return "";
		}
		return filePath;
	}
	
	private static void goCamera(Activity act, String mediaType, int requestCode, Uri uri){
		//check
		if(act == null || requestCode <= 0 || uri == null) return;
		
		
		Intent intent=new Intent(mediaType);
		intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
		intent.putExtra(MediaStore.EXTRA_SIZE_LIMIT,3234567);
		act.startActivityForResult(intent, requestCode);
	}
	
}
